<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\exception\ValidateException;

use app\common\model\{User, Code};
use app\common\validate\UserValidate;

class LoginController extends BaseController
{

    private $userModel;

    public function __construct(User $userModel)
    {
        parent::initialize();
        $this->userModel = $userModel;
    }

    /** 登陆 */
    public function login()
    {
        $data = input('');

        try{
            validate(UserValidate::class)->scene('login')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        /** 查询用户信息 */
        $user_info = $this->userModel->getFind(['phone|doctor_no' => $data['phone']], 'id, password');

        if($user_info['code'] != 'ok'){
            $this->error_back('手机号不存在');
        }

        if(setPass($data['password']) != $user_info['data']['password']){
            $this->error_back('密码错误');
        }

        $this->success_back('登陆成功', $user_info['data']['id']);
    }

    /** 注册 */
    public function register()
    {
        $data = input('');

        try{
            validate(UserValidate::class)->scene('register')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        /** 查询用户信息 */
        $user_info = $this->userModel->getFind(['phone' => $data['phone']], 'id');

        if($user_info['code'] == 'ok'){
            $this->error_back('手机号已存在');
        }

        $codeModel = new Code();

        /** 验证手机号 */
        // $code_flag = $codeModel->checkCode($data['phone'], $data['code']);

        // if($code_flag['code'] != 'ok'){
        //     $this->error_back($code_flag['msg']);
        // }

        /** 添加用户 */
        $flag = $this->userModel->add(['phone' => $data['phone'], 'password' => setPass($data['password'])]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 验证码改为已使用 */
        // $codeModel->edit(['id' => $code_flag['data'], 'is_use' => 1]);

        $this->success_back('注册成功', $flag['data']);
    }

    /** 忘记密码 */
    public function forget()
    {
        $data = input('');

        try{
            validate(UserValidate::class)->scene('forget')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $codeModel = new Code();

        /** 验证手机号 */
        $code_flag = $codeModel->checkCode($data['phone'], $data['code']);

        if($code_flag['code'] != 'ok'){
            $this->error_back($code_flag['msg']);
        }

        /** 修改密码 */
        $flag = $this->userModel->editWhere(['password' => setPass($data['password'])], ['phone' => $data['phone']]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 验证码改为已使用 */
        $codeModel->edit(['id' => $code_flag['data'], 'is_use' => 1]);

        $this->success_back('修改成功');
    }

    /** 发送验证码 */
    public function sendSms(){
        $data = input('post.');

        try{
            validate(UserValidate::class)->scene('phone')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $codeModel = new Code();

        $code_info = $codeModel->getFind(['phone' => $data['phone'], 'is_use' => 0], '*', 'id desc');

        /** 5分钟内重复提交 */
        if (!empty($code_info['data']) && $code_info['data']['expiration_time'] -  5 * 60 > time()){
            $this->error_back("验证码已发送");
        }

        /** 随机验证码 */
        $data['code'] = rand(1111, 9999);

        /** 发送验证码 */
        sendSms($data['phone'], $data['code']);

        /** 过期时间 */
        $data['expiration_time'] = strtotime('+5 minute');

        /** 添加记录 */
        $flag = $codeModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('验证码发送成功');
    }

    /** 随机生成情博士号 */
    public function get_doctor_no()
    {
        $this->success_back('ok', getDoctorNo());
    }
}
